package net.kazed.nextaction.database.serialize;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.xml.sax.Attributes;

public class TaskHandler extends AbstractElementHandler implements ElementHandlerContext<TaskDto> {

    private TaskDto currentTask;
    private List<TaskDto> tasks = new ArrayList<TaskDto>();

    public TaskHandler() {
        super();
        addElementHandler("id", new TaskHandler.TaskIdHandler(this));
        addElementHandler("description", new TaskHandler.TaskDescriptionHandler(this));
        addElementHandler("parentTaskId", new TaskHandler.TaskParentTaskIdHandler(this));
        addElementHandler("details", new TaskHandler.TaskDetailsHandler(this));
        addElementHandler("contextId", new TaskHandler.TaskContextIdHandler(this));
        addElementHandler("complete", new TaskHandler.TaskCompleteHandler(this));
    }

    public TaskHandler(Map<String, ElementHandler> elementHandlers) {
        super(elementHandlers);
        // TODO Auto-generated constructor stub
    }

    public TaskDto getCurrentTask() {
        return currentTask;
    }

    public List<TaskDto> getTasks() {
        return tasks;
    }

    @Override
    public void startElement(String name, Attributes attributes,
            ParserState state) {
        currentTask = new TaskDto();
    }

    @Override
    public void endElement(String name, ParserState state) {
        tasks.add(currentTask);
        currentTask = null;
    }
    
    @Override
    public TaskDto getContextData() {
        return currentTask;
    }

    public static class TaskIdHandler extends AbstractElementHandlerWithContext<TaskDto> {
        public TaskIdHandler(ElementHandlerContext<TaskDto> handlerContext) {
            super(handlerContext);
        }
        @Override
        public void characters(String text, ParserState state) {
            getContextData().id = Integer.valueOf(text);
        }
    }
    
    public static class TaskDescriptionHandler extends AbstractElementHandlerWithContext<TaskDto> {
        public TaskDescriptionHandler(ElementHandlerContext<TaskDto> handlerContext) {
            super(handlerContext);
        }
        @Override
        public void characters(String text, ParserState state) {
            getContextData().description = text;
        }
    }
    
    public static class TaskDetailsHandler extends AbstractElementHandlerWithContext<TaskDto> {
        public TaskDetailsHandler(ElementHandlerContext<TaskDto> handlerContext) {
            super(handlerContext);
        }
        @Override
        public void characters(String text, ParserState state) {
            getContextData().details = text;
        }
    }
    
    public static class TaskParentTaskIdHandler extends AbstractElementHandlerWithContext<TaskDto> {
        public TaskParentTaskIdHandler(ElementHandlerContext<TaskDto> handlerContext) {
            super(handlerContext);
        }
        @Override
        public void characters(String text, ParserState state) {
            getContextData().parentTaskId = Integer.valueOf(text);
        }
    }

    public static class TaskContextIdHandler extends AbstractElementHandlerWithContext<TaskDto> {
        public TaskContextIdHandler(ElementHandlerContext<TaskDto> handlerContext) {
            super(handlerContext);
        }
        @Override
        public void characters(String text, ParserState state) {
            getContextData().contextId = Integer.valueOf(text);
        }
    }

    public static class TaskCompleteHandler extends AbstractElementHandlerWithContext<TaskDto> {
        public TaskCompleteHandler(ElementHandlerContext<TaskDto> handlerContext) {
            super(handlerContext);
        }
        @Override
        public void characters(String text, ParserState state) {
            getContextData().completed = Boolean.parseBoolean(text);
        }
    }

}
